package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Card;
import com.ruoyi.system.domain.Goods;
import com.ruoyi.system.mapper.CardMapper;
import com.ruoyi.system.mapper.GoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.OnlinePickupMapper;
import com.ruoyi.system.domain.OnlinePickup;
import com.ruoyi.system.service.IOnlinePickupService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 在线提货Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-20
 */
@Service
public class OnlinePickupServiceImpl implements IOnlinePickupService 
{
    @Resource
    private OnlinePickupMapper onlinePickupMapper;
    @Resource
    private CardMapper cardMapper;
    @Resource
    private GoodsMapper goodsMapper;

    /**
     * 查询在线提货
     * 
     * @param id 在线提货主键
     * @return 在线提货
     */
    @Override
    public OnlinePickup selectOnlinePickupById(Long id)
    {
        return onlinePickupMapper.selectOnlinePickupById(id);
    }

    /**
     * 查询在线提货列表
     * 
     * @param onlinePickup 在线提货
     * @return 在线提货
     */
    @Override
    public List<OnlinePickup> selectOnlinePickupList(OnlinePickup onlinePickup)
    {
        return onlinePickupMapper.selectOnlinePickupList(onlinePickup);
    }

    /**
     * 新增在线提货
     * 
     * @param onlinePickup 在线提货
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertOnlinePickup(OnlinePickup onlinePickup)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            //查询当前卡号的余额
            Card card = new Card();
            card.setCardNo(onlinePickup.getCardNo());
            List<Card> cards = cardMapper.selectCardList(card);

            /*如果出现重复卡号 进行返回错误状态*/
            if (cards.size() > 1) {
                ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
                ajaxResult.put(AjaxResult.MSG_TAG, "在线提货失败，卡号重复请联系管理员！");
                return ajaxResult;
            }else if(cards.size() == 0){
                ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
                ajaxResult.put(AjaxResult.MSG_TAG, "预约自提失败，卡号不存在请联系管理员！");
                return ajaxResult;
            }

            // 查询房前商品价格
            Goods goods = goodsMapper.selectGoodsById(onlinePickup.getGoodsId());
            /*商品单价*/
            BigDecimal goodsPrice = goods.getGoodsPrice();
            /*商品数量*/
            Integer goodsNum = onlinePickup.getGoodsNum();
            /*商品总价*/
            BigDecimal multiply = goodsPrice.multiply(new BigDecimal(goodsNum));

            // 商品总价与卡余额进行对比
            /*卡余额*/
            BigDecimal price = cards.get(0).getBalance();
            /*卡余额 - 商品总价*/
            BigDecimal result = price.subtract(multiply); // 使用 subtract 方法进行减法运算

            // 如果卡余额小于商品总价
            if (result.compareTo(BigDecimal.ZERO) < 0) {
                ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
                ajaxResult.put(AjaxResult.MSG_TAG, "在线提货失败，卡余额不足！");
                return ajaxResult;
            }

            //余额大于商品总价时 进行礼品卡余额扣减
            cards.get(0).setBalance(result);
            cards.get(0).setUpdateTime(DateUtils.getNowDate());
            cardMapper.updateCard(cards.get(0));

            onlinePickup.setCreateTime(DateUtils.getNowDate());
            int i = onlinePickupMapper.insertOnlinePickup(onlinePickup);

            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_SUCCESS);
            ajaxResult.put(AjaxResult.MSG_TAG, "在线提货成功！");

        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.BUSINESS_FAIL);
            ajaxResult.put(AjaxResult.MSG_TAG, "在线提货失败，请联系管理员！");
        }
        return ajaxResult;

    }

    /**
     * 修改在线提货
     * 
     * @param onlinePickup 在线提货
     * @return 结果
     */
    @Override
    public int updateOnlinePickup(OnlinePickup onlinePickup)
    {
        onlinePickup.setUpdateTime(DateUtils.getNowDate());
        return onlinePickupMapper.updateOnlinePickup(onlinePickup);
    }

    /**
     * 批量删除在线提货
     * 
     * @param ids 需要删除的在线提货主键
     * @return 结果
     */
    @Override
    public int deleteOnlinePickupByIds(Long[] ids)
    {
        return onlinePickupMapper.deleteOnlinePickupByIds(ids);
    }

    /**
     * 删除在线提货信息
     * 
     * @param id 在线提货主键
     * @return 结果
     */
    @Override
    public int deleteOnlinePickupById(Long id)
    {
        return onlinePickupMapper.deleteOnlinePickupById(id);
    }
}
